// pages/me/index.js
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    userInfo: {},
    is_reporter: 1,
    documentTotal: 0,
    liveTotal: 0,
    msgUnread: 0,
    taskDone: 0,
    taskTotal: 0,
    taskUnRead: 0,
    taskUndone: 0,
    useTotal: 0,
    archivesTotal: 0,
    checksTotal: 0,
    rejectionTotal: 0,
    xcTaskUndone: 0,
    showEnroll:0,
    user_role: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    //添加选中效果
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      var tabBar = wx.getStorageSync("tabBar");
      this.getTabBar().setData({
        selected: tabBar.length - 1. //这个数是，tabBar从左到右的下标，从0开始
      })
    }

    //获取用户信息
    http.ApiGet(apiUrl.UrlUserInfo, {}, function (res) {
      // console.log(res);
      if (res.code == 1) {
        var user = res.data.userinfo;
        var taskUnRead = res.data.taskUnRead;
        var msgUnread = res.data.msgUnread;
        that.setData({
          userInfo: user,
          is_reporter: user.is_reporter,
          documentTotal: res.data.documentTotal,
          liveTotal: res.data.liveTotal,
          msgUnread: msgUnread,
          taskDone: res.data.taskDone,
          taskTotal: res.data.taskTotal,
          taskUnRead: taskUnRead,
          taskUndone: res.data.taskUndone,
          useTotal: res.data.useTotal,
          archivesTotal: res.data.archivesTotal,
          checksTotal: res.data.checksTotal,
          rejectionTotal: res.data.rejectionTotal,
          xcTaskUndone: res.data.xcTaskUndone,
          showEnroll:res.data.showEnroll
        });
        wx.setStorageSync("user", user);

        var tabBar = util.setTabBarData(res.data);

        if (typeof that.getTabBar === 'function' &&
          that.getTabBar()) {
          that.getTabBar().setData({
            selected: tabBar.length - 1,
            list: tabBar
          })
        }

        // var openId = wx.getStorageSync("openId");
        // if (!openId) {
        //   wx.showModal({
        //     title: '您还没有微信授权！立即前往？',
        //     success: function (res) {
        //       if (res.confirm) {
        //         wx.navigateTo({
        //           url: '../me/setting'
        //         })
        //       }
        //     }
        //   });
        // }

      } else {
        wx.reLaunch({
          url: '../login/index'
        })
      }
    });

    wx.setNavigationBarColor({
      frontColor: '#ffffff', // 必写项
      backgroundColor: '#3b85e9', // 必写项
    });


    var user_role = wx.getStorageSync("user_role");
    that.setData({
      user_role: user_role
    });
  },
  //事件处理函数
  bindSettingTap: function () {
    console.log("跳转设置页面");
    wx.navigateTo({
      url: '../me/setting'
    })
  },
  bindAvatarTap: function () {
    console.log("跳转用户信息页面");
    wx.navigateTo({
      url: '../me/userdata'
    })
  },
  bindRoleTap: function () {
    var that = this;
    var user_role = wx.getStorageSync("user_role");
    if (user_role == "editor") {
      user_role = "reporter"
    } else {
      user_role = "editor";
    }
    wx.showModal({
      title: '确定切换用户角色？',
      success: function (res) {
        if (res.confirm) {
          wx.setStorageSync("user_role", user_role);
          that.setData({
            user_role: user_role
          });
          if(user_role=="reporter"){
            wx.reLaunch({
              url: '../task/index'
            });
          }else{
            wx.reLaunch({
              url: '../audit/index'
            });
          }
        }
      }
    });
  }
})